package com.qu.waibao.home.nav;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;

import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.utility.UserContext;
import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.base.BaseFragment;
import com.qu.waibao.customer.CustomerFragment;
import com.qu.waibao.home.HomeFragment;
import com.qu.waibao.home.activity.KefuActivity;
import com.qu.waibao.my.fragment.ChezhuFragment;
import com.qu.waibao.my.fragment.MyFragment;
import com.qu.waibao.my.fragment.XiuFragment;
import com.qu.waibao.nearby.NearbyFragment;
import com.qu.waibao.publisher.PublisherFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/21.
 */

public class NavFragment extends BaseFragment implements View.OnClickListener{
    @BindView(R.id.nav_item_home)
    NavigationButton mNavHome;

    @BindView(R.id.nav_item_nearby)
    NavigationButton mNavNearby;

    @BindView(R.id.nav_item_publisher)
    NavigationButton mNavPublish;

    @BindView(R.id.nav_item_customer)
    NavigationButton mNavCustomer;

    @BindView(R.id.nav_item_me)
    NavigationButton mNavMy;

    private Context mContext;
    private int mContainerId;
    private FragmentManager mFragmentManager;
    private NavigationButton mCurrentNavButton;
    private OnNavigationReselectListener mOnNavigationReselectListener;
    private Fragment fragment;
    public NavFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_nav;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        int card = AppConfig.getInt("card");
        YWIMKit mIMKit = YWAPI.getIMKitInstance(AppConfig.getString("userid"), AppConfig.APP_KEY);
        fragment = mIMKit.getChattingFragment("kefu", AppConfig.APP_KEY);
        Bundle fragmentBundle = new Bundle();
        fragmentBundle.putSerializable(UserContext.EXTRA_USER_CONTEXT_KEY, mIMKit.getUserContext());
        mNavHome.init(R.drawable.tab_icon_home,
                R.string.tab_home,
                HomeFragment.class,null);

        mNavNearby.init(R.drawable.tab_icon_nearby,
                R.string.tab_fujin,
                NearbyFragment.class,null);
        if(card == 3 || card == 1) {
            mNavPublish.setVisibility(View.VISIBLE);
            mNavPublish.init(R.drawable.tab_icon_publish,
                    R.string.tab_publish,
                    PublisherFragment.class,null);
        }else {
            mNavPublish.setVisibility(View.GONE);
        }
        //客服
        mNavCustomer.init(R.drawable.tab_icon_customer,
                R.string.tab_kefu,
                 fragment.getClass(),null);
       if(card == 3){
           mNavMy.init(R.drawable.tab_icon_me,
                   R.string.tab_my,
                   MyFragment.class,null);
       }else if(card == 1){
           mNavMy.init(R.drawable.tab_icon_me,
                   R.string.tab_my,
                   ChezhuFragment.class,null);
       }else if(card == 2){
           mNavMy.init(R.drawable.tab_icon_me,
                   R.string.tab_my,
                   XiuFragment.class,null);
       }
    }

    @OnClick({R.id.nav_item_home, R.id.nav_item_nearby,
            R.id.nav_item_publisher, R.id.nav_item_customer,
            R.id.nav_item_me})
    @Override
    public void onClick(View v) {
        if (v instanceof NavigationButton) {
            NavigationButton nav = (NavigationButton) v;
            doSelect(nav);
           /* if(nav.getTabName().equals("客服")){
                nav.setSelected(true);
                startActivity(new Intent(mContext, KefuActivity.class));
            }else {
                doSelect(nav);
            }*/
        }
    }

    public void setup(Context context, FragmentManager fragmentManager, int contentId, OnNavigationReselectListener listener) {
        mContext = context;
        mFragmentManager = fragmentManager;
        mContainerId = contentId;
        mOnNavigationReselectListener = listener;
        // do clear
        clearOldFragment();
        // do select first
        doSelect(mNavHome);
    }


    @SuppressWarnings("RestrictedApi")
    private void clearOldFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0)
            return;
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != this && fragment != null) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit)
            transaction.commitNow();
    }

    private void doSelect(NavigationButton newNavButton) {
        NavigationButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            if (oldNavButton == newNavButton) {
                onReselect(oldNavButton);
                return;
            }
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton);
        mCurrentNavButton = newNavButton;
    }

    private void doTabChanged(NavigationButton oldNavButton, NavigationButton newNavButton) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.detach(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            if (newNavButton.getFragment() == null) {
                if(newNavButton.getTabName().equals("客服")) {
                    ft.add(mContainerId, fragment);
                    newNavButton.setFragment(fragment);
                }else {
                    Fragment fragment = Fragment.instantiate(mContext,
                            newNavButton.getClx().getName(), newNavButton.getBundle());
                    ft.add(mContainerId, fragment);
                    newNavButton.setFragment(fragment);
                }
            } else {
                ft.attach(newNavButton.getFragment());
            }
        }
        ft.commit();
    }



    private void onReselect(NavigationButton navigationButton) {
        OnNavigationReselectListener listener = mOnNavigationReselectListener;
        if (listener != null) {
            listener.onReselect(navigationButton);
        }
    }

    public interface OnNavigationReselectListener {
        void onReselect(NavigationButton navigationButton);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void initData() {
        super.initData();
    }
}

